package com.opener.common.http;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author baboy
 */
public class URLUtils {
    public static String format(String url, Map<String, String> param) {
        String newStr = url;
        try {
            if (param != null) {
                String re = "\\{([^\\{\\}]+)\\}";
                Pattern p = Pattern.compile(re);
                Matcher m = p.matcher(newStr);
                while (m.find()) {
                    String k2 = m.group(1)+"";
                    String ph = "{" + k2 + "}";
                    Object v = "";
                    if (param.containsKey(k2)) {
                        v = param.get(k2);
                        v = v == null ? "":String.valueOf(v);
                    }
                    newStr = newStr.replace(ph, (String)v );
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return newStr;
    }

}
